# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QSizePolicy, QVBoxLayout
from qfluentwidgets import SimpleCardWidget, Pivot

from components import SlidingStackedWidget
from .captcha_login_widget import CaptchaLoginWidget
from .qrcode_widget import QRCodeWidget
from .user_login_widget import UserLoginWidget


class RightWidget(SimpleCardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.pivot = Pivot(self)
        self.stackedWidget = SlidingStackedWidget(self)
        self.loginWidget = UserLoginWidget(self.stackedWidget)
        self.captchaWidget = CaptchaLoginWidget(self.stackedWidget)
        self.qrcodeWidget = QRCodeWidget(self.stackedWidget)

        self.__initWidgets()

    def __initWidgets(self):
        self.setSizePolicy(QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.MinimumExpanding)
        self.setMinimumSize(400, 400)
        # self.setMaximumSize(528, 604)

        self.pivot.addItem("login", "账号密码登录")
        self.pivot.addItem("captcha", "验证码登录")
        self.pivot.addItem('qrcode', '扫码登录', onClick=self.qrcodeWidget.initCode)
        self.stackedWidget.addWidget(self.loginWidget)
        self.stackedWidget.addWidget(self.captchaWidget)
        self.stackedWidget.addWidget(self.qrcodeWidget)

        self.verticalLayout.setContentsMargins(50, 50, 50, 50)
        self.verticalLayout.addWidget(self.pivot, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.stackedWidget)

        self.pivot.currentItemChanged.connect(self._onPivotItemChanged)
        self.pivot.setCurrentItem("login")

    def _onPivotItemChanged(self, routeKey: str):
        params = {
            "login": self.loginWidget,
            "captcha": self.captchaWidget,
            "qrcode": self.qrcodeWidget,
        }
        self.stackedWidget.setCurrentWidget(params[routeKey])
